package org.jeecg.modules.yusuannz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.yusuannz.entity.NianDuHuiZongNz;
import org.jeecg.modules.yusuannz.service.ICaiwuBiaozhunNzService;
import org.jeecg.modules.yusuannz.entity.CaiwuBiaozhunNz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @Author: jeecg-boot
* @Date:   2020-06-15
* @Version: V1.0
*/
@RestController
@RequestMapping("/yusuan/biaozhunnz")
@Slf4j
public class CaiwuBiaozhunNzController extends JeecgController<CaiwuBiaozhunNz, ICaiwuBiaozhunNzService> {
   @Autowired
   private ICaiwuBiaozhunNzService caiwuBiaozhunNzService;
   @Autowired
   private ISysDepartService sysDepartService;

   @PostMapping("/addlist")
   public Result<?> addList(@RequestBody List<CaiwuBiaozhunNz> zhijieList){
       this.caiwuBiaozhunNzService.saveOrUpdateBatch(zhijieList);
       return Result.ok("操作成功");
   }

    /**
     * 加载所有数据
     */
    @RequestMapping(value = "/loadData", method = RequestMethod.GET)
    public Result<List<CaiwuBiaozhunNz>> loadTreeRoot(@RequestParam( value = "deptId",required = false)String deptId,@RequestParam(name="pid") String pid) {
        if(StringUtils.isBlank(deptId)){
            String orgCode = ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getOrgCode();
            deptId = this.sysDepartService.getOne(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getOrgCode,orgCode)).getId();
        }
        Result<List<CaiwuBiaozhunNz>> result = new Result<List<CaiwuBiaozhunNz>>();
        try {
            List<CaiwuBiaozhunNz> ls = this.caiwuBiaozhunNzService.listData(deptId,pid);
            loadDataChildren(deptId,ls);
            result.setResult(ls);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 递归求子节点 同步加载用到
     */
    private void loadDataChildren(String deptId,List<CaiwuBiaozhunNz> ls) {
        for (CaiwuBiaozhunNz tsm : ls) {
            List<CaiwuBiaozhunNz> temp = this.caiwuBiaozhunNzService.listData(deptId,tsm.getTreeId());
            if(temp!=null && temp.size()>0) {
                tsm.setChildren(temp);
                loadDataChildren(deptId,temp);
            }
        }
    }

    @GetMapping("/nianduhuizong")
    public Result<?> nianduhuizong(@RequestParam(name="deptId",required = false) String deptId){
        String orgCode = "";
        if(StringUtils.isBlank(deptId)){
            orgCode = ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getOrgCode();
        }else{
            orgCode = this.sysDepartService.getById(deptId).getOrgCode();
        }
        List<NianDuHuiZongNz> list = this.caiwuBiaozhunNzService.nianduhuizong(orgCode);
        return Result.ok(list);
    }
}
